package com.jk.smartelderlycare.controller;

import com.jk.smartelderlycare.dao.MessageRepository;
import com.jk.smartelderlycare.entity.TbMessage;
import com.jk.smartelderlycare.pojo.ResponseResult;
import com.jk.smartelderlycare.security.LoginUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * @author hhk
 * @version 1.0
 * @ClassName TbMessageController
 * @program: smart-elderly-care
 * @description: TODO
 * @date 2024/11/27 19:22
 */
@Api(tags = "消息管理模块")
@Slf4j
@RestController
@RequestMapping(value = "message")
public class TbMessageController {
    @Autowired
    private MessageRepository messageRepository;
    @ApiOperation(value = "添加消息", httpMethod = "POST", notes = "添加消息")
    @PostMapping(value = "addMessage")
    private ResponseResult addMessage(@RequestBody TbMessage tbMessage) {
        try {
            LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            tbMessage.setName(loginUser.getUser().getUsername());
            tbMessage.setType("1");
            tbMessage.setTime(new Date());
            messageRepository.save(tbMessage);
            return new ResponseResult(200,"添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(500,"添加失败");
        }
    }
    @ApiOperation(value = "根据房屋id查询消息", httpMethod = "POST", notes = "根据房屋id查询消息")
    @PostMapping(value = "MessageListByHomestayId")
    private ResponseResult<List<TbMessage>> MessageListByHomestayId(@RequestBody TbMessage tbMessage) {
        LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        List<TbMessage> MessageList = messageRepository.findByNameAndHomestayId(loginUser.getUser().getUsername(), tbMessage.getHomestayId());
        return new ResponseResult<>(200,"查询成功",MessageList);
    }












}
